package com.dy.lock.screen.component.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.dy.lock.screen.component.CustomerMaterialCollectComponent;
import com.dy.lock.screen.constants.TransmissionCode;
import com.dy.lock.screen.exception.ServerDisponseException;
import com.dy.lock.screen.pojo.MaterialCollectInfo;
import com.dy.lock.screen.scoket.Head;
import com.dy.lock.screen.service.MaterialCollectService;
import com.dy.lock.screen.utils.CommonUtil;
import com.dy.lock.screen.vo.MaterialCollectVo;
import com.google.gson.Gson;

@Component("customerMaterialCollectComponent")
public class CustomerMaterialCollectComponentImpl implements
		CustomerMaterialCollectComponent {

	private static final Logger log = Logger
			.getLogger(CustomerMaterialCollectComponentImpl.class);
	private Gson gson;

	@Resource(name = "materialCollectService")
	private MaterialCollectService materialCollectService;

	public CustomerMaterialCollectComponentImpl() {
		gson = CommonUtil.gson();
	}

	@Override
	public boolean collectOrCancelMaterial(String body, Head head) {
		MaterialCollectVo vo = gson.fromJson(body, MaterialCollectVo.class);
		if (vo != null) {
			MaterialCollectInfo collectInfo = new MaterialCollectInfo();
			collectInfo.setUserId(vo.getUserId());
			collectInfo.setMaterialId(vo.getMaterialId());
			int result = -1;
			if (vo.isCollectOrNot()) {
				MaterialCollectInfo cli = this.materialCollectService
						.queryByMidAndCid(collectInfo);
				if (cli == null) {
					result = this.materialCollectService.save(collectInfo);
				} else {
					log
							.info("requestId:" + head.getRequestId()
									+ "用户已经收藏过此文章。");
					throw new ServerDisponseException(
							TransmissionCode.MATERIAL_COLLECT.getCode(),
							TransmissionCode.MATERIAL_COLLECT.getMessage());
				}
			} else {
				result = this.materialCollectService.delete(collectInfo);
				if (result <= 0) {
					log.info("requestId:" + head.getRequestId()
							+ "用户对此文章还没有收藏，不能取消收藏。");
					throw new ServerDisponseException(
							TransmissionCode.NOT_COLLECT_CAN_NOT_CANCEL
									.getCode(),
							TransmissionCode.NOT_COLLECT_CAN_NOT_CANCEL
									.getMessage());
				}
			}
			if (result > 0) {
				return true;
			} else {
				return false;
			}
		} else {
			log.info("requestId:" + head.getRequestId() + ",用户收藏素材操作时，报文解析异常。");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST
					.getCode(), TransmissionCode.ERROR_ADJUST.getMessage());
		}

	}

	@Override
	public List<MaterialCollectInfo> list(String body, Head head) {
		MaterialCollectVo vo = gson.fromJson(body, MaterialCollectVo.class);
		if (vo != null) {
			Integer currentPage = vo.getPageIndex();
			Integer pageSize = vo.getPageCount();
			Integer pageStart = 1;
			Integer pageEnd = 10;
			if (currentPage != null && pageSize != null) {
				pageStart = (currentPage - 1) * pageSize + 1;
				pageEnd = currentPage * pageSize;
			}
			Map<String, Object> paraMap = new HashMap<String, Object>();
			paraMap.put("pageStart", pageStart);
			paraMap.put("pageEnd", pageEnd);
			paraMap.put("userId", vo.getUserId());
			paraMap.put("materialTag", vo.getAdTagType());
			List<MaterialCollectInfo> materialList = this.materialCollectService
					.queryPage(paraMap);
			List<MaterialCollectInfo> maList = new ArrayList<MaterialCollectInfo>();
			if (materialList != null && !materialList.isEmpty()) {
				for (MaterialCollectInfo info : materialList) {
					if (StringUtils.isNotBlank(info.getMaterialPic())) {
						info.setMaterialPic(CommonUtil.getUrlPrefix()
								+ info.getMaterialPic());
					}
					maList.add(info);
				}
				return maList;
			} else {
				return new ArrayList<MaterialCollectInfo>();
			}

		} else {
			log.info("requestId:" + head.getRequestId() + ",获取素材收藏列表时，报文解析异常。");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST
					.getCode(), TransmissionCode.ERROR_ADJUST.getMessage());
		}
	}

}
